package com.guigu.buy.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.guigu.buy.dto.Product_Supplier;
import com.guigu.buy.pojo.Product;
import com.guigu.buy.pojo.PurchaseOrder;
import com.guigu.buy.pojo.PurchaseOrderDetails;
import com.guigu.buy.service.IProductService;
import com.guigu.buy.utils.FileAddUtil;
import com.guigu.buy.vo.ProductVo;
import com.guigu.buy.vo.ProductVo2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author com.guigu
 * @since 2024-05-15
 */
@CrossOrigin
@Controller
@RequestMapping("/buy/product")
public class ProductController {
    @Autowired
    private IProductService productService;
    @Autowired
    private FileAddUtil addUtil;
    /**
     * 添加商品
     * @return
     */
    @RequestMapping("/addProduct2")
    @ResponseBody
    public Boolean addProduct2(@RequestBody Product product){
        String productId= addUtil.getTypeNumber("700");
        product.setId(null);
        product.setProductId(productId);
        product.setCreateTime(new Date());
        product.setUpdateTime(new Date());
        product.setStatus("0");
        product.setStock(0);
        product.setProductInventory(0);
        product.setProductStatus("01");
        product.setImgUrl(product.getImgUrl());
        System.out.println(product);
        return productService.save(product);
    }
    @RequestMapping("/myupload")
    @ResponseBody
    public String myupload(MultipartFile file){
        //获取原文件名
        String originalFilename=file.getOriginalFilename();
        //截取后缀名
        String suffix=originalFilename.substring(originalFilename.lastIndexOf('.'));
        //新文件名要唯一
        String newFileName= UUID.randomUUID().toString()+suffix;
        //该文件要放在指定目录下
        File dir = new File("D:/img");
        if (!dir.exists())
        dir.mkdir();
        //目标文件对象
        File destFile =new File(dir,newFileName);
        //文件写入
        try {
            file.transferTo(destFile);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "http://localhost:10002/"+newFileName;
    }
    /**
     * 添加库存
     * @return
     */
    @RequestMapping("/addKuCun2")
    @ResponseBody
    public Boolean addKuCun2(@RequestBody Product product){

        return productService.updateById(product);
    }
    /**
     * 添加库存 查看
     * @return
     */
    @RequestMapping("/addKuCun/{id}")
    @ResponseBody
    public Product addKuCun(@PathVariable int id){
        Product product=new Product();
        product=productService.getById(id);
        return product;
    }
    /**
     * 上下架商品
     * @return
     */
    @RequestMapping("/status")
    @ResponseBody
    public Boolean status(@RequestBody Product product){

        return productService.updateById(product);
    }
    /**
     * 删除商品
     * @return
     */
    @RequestMapping("/delProduct/{id}")
    @ResponseBody
    public Boolean delProduct(@PathVariable int id){
//        id+=id+1;

        return productService.delProduct(id);
    }
    //查询所有商品
    @RequestMapping("/list/{pageSize}/{pageNo}")
    @ResponseBody
    public PageInfo<Product> list(@PathVariable int pageSize,
                                  @PathVariable int pageNo,
                                  @RequestBody ProductVo vo){
    return productService.queryAll(pageNo,pageSize,vo);
}
    //查询所有商品
    @RequestMapping("/selectAll/{pageSize}/{pageNo}")
    @ResponseBody
    public PageInfo<Product> selectAll(@PathVariable int pageSize,
                                  @PathVariable int pageNo,
                                  @RequestBody ProductVo2 vo){
        System.out.println(vo);
        return productService.selectAll(pageNo,pageSize,vo);
    }
    //根据商品编号查询商品(带供应商)
    @RequestMapping("/getOne/{id}")
    @ResponseBody
    public Product_Supplier getOne(@PathVariable int id){
        return productService.queryOne(id);
    }
    //分类查询不同的商品
    @RequestMapping("/sortProduct/{id}")
    @ResponseBody
    public List<Product> listSortProduct(@PathVariable int id){
        List<Product> list=productService.listSortProduct(id);
        System.out.println("1111"+list);
        return list;
    }
    /**
     * 主页文本框 模糊查询商品
     * @return
     */
    @RequestMapping("/selectProduct")
    @ResponseBody
    public List<Product> list(@RequestBody String input){
       String productName= input.substring(10,input.length()-2);
        List<Product> list=productService.select(productName);
        return list;
    }
    //主页查询推荐商品 只查今年的商品
    @RequestMapping("/selectFood")
    @ResponseBody
    public List<Product> listFood(){
        List<Product> list=productService.listRecommended();
        return list;
    }
    //促销商品 所有商品卖的最少的前五个
    @RequestMapping("/selectClothes")
    @ResponseBody
    public List<Product> listClothes(){
        List<Product> list=productService.listClothes();
        return list;
    }
    //商品详情
    @RequestMapping("/getOneProduct/{id}")
    @ResponseBody
    public List<Product> getOneProduct(@PathVariable int id){
        System.out.println("id:"+id);
        QueryWrapper<Product> wrapper=new QueryWrapper<>();
        wrapper.eq("id",id);
        List<Product> product=productService.list(wrapper);
        return product;
    }

    //根据商品名字查询

    @RequestMapping("/listPagelist/{pageSize}/{pageNo}")
    @ResponseBody
    public PageInfo<Product> listPagelist(@PathVariable int pageSize,
                                  @PathVariable int pageNo,
                                  @RequestBody Product productName){
        System.out.println(productName);
        return productService.listPage(productName,pageNo,pageSize);
    }

    //根据商品编号查询详情商品显示对话框

    @RequestMapping("/buyid/{id}")
    @ResponseBody
    public List<Product> buyId(@PathVariable int id
    ){
        List<Product> list=productService.buyId(id);
        return list;
    }


    /**
     * 根据ID获取商品信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Product getProductById(@PathVariable Integer id) {
        return productService.getProductById(id);
    }
}
